package jinyilw.swing.visuals.Rcomp;

import jinyilw.swing.rlaf.ui.RRootPaneUI;

import javax.swing.JWindow;
import javax.swing.plaf.RootPaneUI;
import java.awt.Image;
import java.awt.Window;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;

public class RWindow extends JWindow
{
	private static final long serialVersionUID = 716505333572120038L;
	private RRootPaneUI rRootPaneUI = null;
	protected boolean rounded = true;

	public RWindow()
	{
		initGUI();
	}

	public RWindow(Window owner)
	{
		super(owner);
		initGUI();
	}

	private void initGUI()
	{
		RootPaneUI ui = getRootPane().getUI();
		if (ui instanceof RRootPaneUI)
			rRootPaneUI = (RRootPaneUI) ui;
	}

	public void setCustomBGImage(Image img)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setCustomImage(img);
	}

	public void setCustomBGImage(Image img, int x, int y, int w, int h)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setCustomImage(
					((BufferedImage) img).getSubimage(x, y, w, h));
	}

	public boolean isRounded()
	{
		return rounded;
	}

	public void setRounded(boolean b)
	{
		rounded = b;
		if (b)
			setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(),
					5, 5));
		else
			setShape(null);
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		super.setBounds(x, y, width, height);
		if (width == 0 || height == 0)
			return;
		if (rounded)
			setShape(new RoundRectangle2D.Double(0, 0, width, height, 5, 5));
	}

	public void setOpacity(int opacity)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setBrightness(opacity);
	}
}
